#include "klippy/core.h"
#include "klippy/logging.h"
#include "klippy/config.h"
#include <iostream>
#include <stdexcept>
#include <cstring>
#include <string>
#include <vector>

// 版本信息
const char* VERSION = "1.0.0";
const char* BUILD_DATE = __DATE__;
const char* BUILD_TIME = __TIME__;

// 命令行帮助
void usage() {
    std::cerr << "Usage: klippy_cpp [options] config_file\n";
    std::cerr << "Options:\n";
    std::cerr << "  -v, --version   Show version information\n";
    std::cerr << "  -h, --help      Show this help message\n";
}

// 版本信息
void show_version() {
    std::cout << "Klippy C++ Version " << VERSION << "\n";
    std::cout << "Build date: " << BUILD_DATE << " " << BUILD_TIME << "\n";
}

int main(int argc, char *argv[]) {
    try {
        // 解析命令行参数
        std::string config_file;
        for (int i = 1; i < argc; i++) {
            if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
                usage();
                return 0;
            } else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) {
                show_version();
                return 0;
            } else if (argv[i][0] == '-') {
                std::cerr << "Unknown option: " << argv[i] << "\n";
                usage();
                return 1;
            } else {
                config_file = argv[i];
            }
        }

        if (config_file.empty()) {
            std::cerr << "Error: No configuration file specified\n";
            usage();
            return 1;
        }

        // 初始化日志系统
        auto& logger = klippy::Logger::get_instance();
        logger.set_level(klippy::Logger::INFO);

        // 创建并初始化打印机
        klippy::Printer printer;
        
        // 加载配置文件
        auto config = klippy::Config::load(config_file);
        
        // 初始化打印机
        printer.initialize();

        // 运行主循环
        printer.run();

        return 0;
    } catch (const std::exception& e) {
        std::cerr << "Fatal error: " << e.what() << std::endl;
        return 1;
    }
}